<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/4
 * Time: 14:33
 */
class ArticleAction extends BaseAction
{
    function index()
    {
        $art = new article();
        $ajax = Req::get('ajax');
        if ($ajax) {
            $page = Req::get('p', 'intval') ? Req::get('p', 'intval') : 1;
            $list = $art->get_list('tid=2&show=10&field=con&uid=' . $this->info['masterid'] . '&pg=' . $page);
            $result = array();

            foreach ($list as $v) {
                $result[] = array('date' => date('Y-m-d', $v->startdate), 'title' => $v->title, 'con' => cn_substr(strip_tags($v->con), 100), 'litpic' => $v->litpic);
            }
            echo json_encode($result);
        } else {
            $list = $art->get_list('tid=2&show=10&field=con&uid=' . $this->info['masterid']);
            $this->assign('list', $list);
            $this->assign('ajax', url(array('c' => 'article', 'ajax' => 1)));
            $this->assign('add', url(array('c' => 'article', 'a' => 'add')));
            $this->display('article');
        }

    }

    function add()
    {
        if ($_POST) return $this->add_do();
        $this->assign('back', url(array('c' => 'article')));
        $this->display('article_add');
    }

    private function add_do()
    {
        $a = Req::post('a');
        $data = array('status' => 'err');
        if (!$a['title']) {
            $data['msg'] = '工作内容不能为空!';
        } else {
            if ($_FILES['file']['name']) {
                $file_path = $this->config['k_upload'] . date('Y') . '/' . date('m') . '/';
                $img = $this->upload($file_path);
                if ($img[0]) $filename = $file_path . $img[0]['savename'];
            }
            $a['litpic'] = $filename;
            $a['typeid'] = 2;
            $a['uid'] = $this->info['masterid'];
            $a['startdate'] = time();
            $a['writer'] = $this->info['nick'];
            $sql = 'select company_id from __role where roleid=' . $this->info['roleid'];
            $company = db::get_one($sql);
            $a['click'] = $company->company_id;

            if (db::_insert('article', $a)) {
                $data = array('status' => 'ok', 'msg' => '提交成功!');
            } else {
                $data['msg'] = '发生未知的错误,未能提交';
            }
        }
        if ($data['status'] == 'err') show_msg($data['msg']);
        show_msg($data['msg'], url(array('c' => 'article')));
        //echo json_encode($data);
    }

    function view($id)
    {
        $id = Req::get('id') ? Req::get('id') : show_msg('参数错误');
        $art = new article();
        $oj = $art->get_one($id);
        $this->assign('title', $oj->title);
        $this->assign('oj', $oj);
        $this->assign('add', url(array('c' => 'article', 'a' => 'del', 'id' => $id)));
        $this->display('article_view');
    }

    private function upload($file_path)
    {

        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize = 9145728;// 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = $file_path;// 设置附件上传目录
        $upload->saveRule = time() . mt_rand(100, 999); // 采用时间戳命名


        if (!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        } else {// 上传成功
            //$this->success('上传成功！');
            return $info = $upload->getUploadFileInfo();
        }
    }

    public function del()
    {
        $id=Req::get('id','intval')?Req::get('id','intval'):show_msg('没有此资料');
        $sql='select uid from __article where id='.$id;

        $res=db::get_one($sql);
        if($this->info['masterid']==$res->uid){
            db::_del('article','id='.$id);
        }
        show_msg('删除成功!','?c=article');
    }
}